package com.example.springbootzk.controller;

import com.example.springbootzk.utils.CuratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/4/7 9:40
 */
@RestController
@RequestMapping("/curator")
public class CuratorController {
    @Autowired
    private CuratorUtils curatorUtils;

    //创建
    @RequestMapping("/create")
    public String createNode(String path, String value) {
        String result = null;
        try {
            result = curatorUtils.createNode(path, value);
        } catch (Exception exception) {
            result = exception.getMessage();
        }
        return result;

    }

    //设置
    @RequestMapping("/set")
    public Object setData(String path, String value) {
        Object result = null;
        try {
            result = curatorUtils.setData(path, value);
        } catch (Exception exception) {
            result = exception.getMessage();
        }
        return result;

    }

    //获取
    @RequestMapping("/get")
    public String getData(String path) {
        String data = null;
        try {
            data = curatorUtils.getData(path);
        } catch (Exception exception) {
            data = exception.getMessage();
        }
        return data;
    }
    //get conf
    @RequestMapping("/conf")
    public Object getConf() {
        return  curatorUtils.getConfmap();
    }

    //删除
    @RequestMapping("/del")
    public String deleteData(String path) {
        String result = "成功";
        try {
            curatorUtils.deleteData(path);
        } catch (Exception exception) {
            result = exception.getMessage();
        }
        return result;
    }

    //NodeCache 监听一个节点，子节点也不关心
    @RequestMapping("/nodeCache")
    public void NodeCache(String path) {
        try {
            curatorUtils.NodeCache(path);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    //监听一个节点和其子节点，孙子节点不监听
    @RequestMapping("/pathChildrenCache")
    public void PathChildrenCache(String path){
        try {
            curatorUtils.PathChildrenCache(path);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

}
